<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - trackball controls</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			html, body {
				width: 100%;
				height: 100%;
				overflow: hidden;
			}
			
			body {
				color: #000;
				font-family:Monospace;
				font-size:13px;
				text-align:center;
				font-weight: bold;
				background-color: #fff;
				margin: 0px;
			}
			#info {
				color:#000;
				position: absolute;
				top: 0px; width: 100%;
				padding: 5px;
				box-sizing: border-box;
			}
			a {
				color: red;
			}
		</style>
	</head>

	<body>
		<div id="info">
			<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - trackball controls example<br />
			MOVE mouse &amp; press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
		</div>

		<script src="./threejs/three.js"></script>

		<script src="./threejs/controls/TrackballControls.js"></script>

		<!-- <script src="js/WebGL.js"></script>
		<script src="js/libs/stats.min.js"></script> -->

		<script>
			// if ( WEBGL.isWebGLAvailable() === false ) {
			// 	document.body.appendChild( WEBGL.getWebGLErrorMessage() );
			// }

			var camera, controls, scene, renderer, stats;
			init();
			animate();
			function init() {
				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
				camera.position.z = 500;
				controls = new THREE.TrackballControls( camera );
				controls.rotateSpeed = 1.5;
				controls.zoomSpeed = 1.2;
				controls.panSpeed = 0.8;
				controls.noZoom = false;
				controls.noPan = false;
				controls.staticMoving = true;
				controls.dynamicDampingFactor = 0.3;
				controls.keys = [ 65, 83, 68 ];

				controls.addEventListener( 'change', render );
				// world
				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0xcccccc );
				// scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
				
				// lights
				var light = new THREE.DirectionalLight( 0xffffff );
				light.position.set( 1, 1, 1 );
				scene.add( light );
				var light = new THREE.DirectionalLight( 0x002288 );
				light.position.set( - 1, - 1, - 1 );
				scene.add( light );
				var light = new THREE.AmbientLight( 0x222222 );
				scene.add( light );

				var cubes = SimpleCube(0,0,0,3,50,['#ff6b02', '#dd422f',
                            '#ffffff', '#fdcd02',
                            '#3d81f7', '#019d53']);
                
                cubes.forEach(cube=>{
                    scene.add(cube);
                })

				// renderer
				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
                document.body.appendChild( renderer.domElement );
                
				// stats = new Stats();
                // document.body.appendChild( stats.dom );
                
				//
				window.addEventListener( 'resize', onWindowResize, false );
				//
				render();
			}

			function onWindowResize() {
				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();
				renderer.setSize( window.innerWidth, window.innerHeight );
				controls.handleResize();
				render();
			}

			function animate() {
				requestAnimationFrame( animate );
				
				controls.update();
				
				// stats.update();
			}

			function render() {
				renderer.render( scene, camera );
			}

			function SimpleCube(x, y, z, num, len, colors) {
				//魔方左上角坐标
				var leftUpX = x - num/2*len;
				var leftUpY = y + num/2*len;
				var leftUpZ = z + num/2*len;

				var cubes = [];
				for (var i = 0; i < num; i++) {
					for (var j = 0; j < num * num; j++) {

						var myFaces = [];
						for (var k = 0; k < 6; k++) {
							myFaces[k] = faces(colors[k]);
						}

						var materials = [];
						for (var k = 0; k < 6; k++) {
							var texture = new THREE.Texture(myFaces[k]);
							texture.needsUpdate = true;
							materials.push(new THREE.MeshLambertMaterial({ map: texture}));
						}

						var cubegeo = new THREE.BoxGeometry(len, len, len);
						var cube = new THREE.Mesh(cubegeo, materials);

						//依次计算各个小方块中心点坐标
						cube.position.x = (leftUpX + len / 2) + (j % num) * len;
						cube.position.y = (leftUpY - len / 2) - parseInt(j / num) * len;
						cube.position.z = (leftUpZ - len / 2) - i * len;
						cubes.push(cube)
					}
				}
				
				return cubes;
			}
			
			function faces(rgbaColor) {
				var canvas = document.createElement('canvas');
				canvas.width = 256;
				canvas.height = 256;
				var context = canvas.getContext('2d');
				//画一个宽高都是256的黑色正方形
				context.fillStyle = 'rgba(0,0,0,1)';
				context.fillRect(0, 0, 256, 256);
				//在内部用某颜色的16px宽的线再画一个宽高为224的圆角正方形并用改颜色填充
				context.rect(16, 16, 224, 224);
				context.lineJoin = 'round';
				context.lineWidth = 16;
				context.fillStyle = rgbaColor;
				context.strokeStyle = rgbaColor;
				context.stroke();
				context.fill();
				return canvas;
			}
		</script>

	</body>
</html>